package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.entity.vo.OrderStatusRecordVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.enums.OrderStatusEnum;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.OrderService;
import cn.explink.dop.tools.GetHttpConnect;
import cn.explink.dop.tools.GetProjectUrl;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.core.util.DataUtils;

/**
 *
 *
 * @author songkaojun 2015年3月20日
 */
@Controller
@RequestMapping("/manualDistributeController")
public class ManualDistributeController {

	@Autowired
	private DeliveryManService deliveryManService;

	@Autowired
	private OrderService orderService;

	@RequestMapping(params = "distribute")
	@ResponseBody
	public AjaxJson distribute(String deliveryManId, @RequestParam(value = "orderIdList[]", required = false) List<String> orderIdList) {
		AjaxJson j = new AjaxJson();
		try {			
			if ((null == deliveryManId) || (orderIdList == null) || (orderIdList.size() == 0)) {
				j.setSuccess(false);
				return j;
			}
			DeliveryManVO deliveryManVO = this.deliveryManService.queryById(deliveryManId);
			List<OrderVO> orderVOList = this.orderService.getByIdList(orderIdList);
			List<OrderStatusRecordVO> recordList = new ArrayList<OrderStatusRecordVO>();
			Date sysDate = this.orderService.getSysDate();
			for (OrderVO orderVO : orderVOList) {
				orderVO.setDeliveryMan(deliveryManVO);
				orderVO.setDeliveryManName(deliveryManVO.getDelName());
				orderVO.setStatus(OrderStatusEnum.ASSIGNED.getValue());
				orderVO.setReadyStatus(1);
				orderVO.setStatusChangeTime(this.orderService.getSysDate());
				orderVO.setUpdateDate(this.orderService.getSysDate());
				orderVO.setPostmanId(orderVO.getDeliveryMan().getPostmanid());
				orderVO.setDeliveryManPhone(orderVO.getDeliveryMan().getDelTelephone());
				// 状态改为“已分配待确认”
				orderVO.setStatusLog(orderVO.getStatusLog() + " 分单方式：手动分单  分单时间:" + DataUtils.date2Str(DataUtils.datetimeFormat) + " 状态：" + OrderStatusEnum.ASSIGNED.getText());
				// TODO batch
				this.orderService.saveOrUpdate(orderVO);
				OrderStatusRecordVO record = new OrderStatusRecordVO();
				record.setChangTime(sysDate);
				record.setOrderId(orderVO.getId());
				record.setRecordDesc("已分配给配送员： " + deliveryManVO.getDelName());
				recordList.add(record);
			}
			this.orderService.batchSave(recordList);
			/**
			 * 	保存成功后，给快递员推送通知
			 *  请求接口如:http://api.neolix.cn/open/push/new_order?token=ac5f97115cd4df280e37871c7569d862&sign=NEW_ORDER_O2O&postman_id=41&out_trade_no=1375
			 */
			this.newOrderPushMessage(recordList, deliveryManVO);
		} catch (Exception e) {
			e.printStackTrace();
		}

		j.setSuccess(true);
		return j;
	}
	/**
	 * 
	* @Title: newOrderPushMessage 
	* @Description: 订单分配之后，给配送员发送消息的方法
	* @author 小强
	* @param @param recordList
	* @param @param deliveryManVO    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void newOrderPushMessage(List<OrderStatusRecordVO> recordList, DeliveryManVO deliveryManVO){
		for(OrderStatusRecordVO temp : recordList ){
			boolean flag = true;
			String param = "token=ac5f97115cd4df280e37871c7569d862";
			
			if(deliveryManVO != null && deliveryManVO.getPostmanid() != null && !"".equals(deliveryManVO.getPostmanid())){//如果这个人存在，并且postman_id也存在且不为空
				param += "&postman_id=" +deliveryManVO.getPostmanid();
				if(temp.getOrderId() != null && !"".equals(temp.getOrderId())){//如果订单的订单id存在且不为空
					param += "&out_trade_no=" + temp.getOrderId();
				}
				else{
					flag = false;
				}
			}else{
				flag = false;
			}
											
			if(flag){
				String url = GetProjectUrl.getNewOrderUrl();
				String results = GetHttpConnect.sendGet(url, param);
				if(results.contains("ok")){
					flag = true;
				}						
			}
			if(!flag){
				//TODO 如果信息推送不成功，那么......
			}				
		}
	}
}
